package com.minelx.statementsplus.cha.base;

import com.alibaba.fastjson.JSONObject;

public class DoWorkResponse {

	private final String message;

	private final int bval;

	private final String content;

	private final boolean isError;

	private final boolean isEnd;

	private DoWorkResponse(String message, int bval, String content, boolean isError, boolean isEnd) {
		this.message = message;
		this.bval = bval;
		this.content = content;
		this.isError = isError;
		this.isEnd = isEnd;
	}

	public String message() {
		return message;
	}

	public int bval() {
		return bval;
	}

	public String content() {
		return content;
	}

	public boolean error() {
		return isError;
	}

	public boolean end() {
		return isEnd;
	}

	public static DoWorkResponse create(String content,
										String message,
										int bval,
										boolean isError,
										boolean isEnd) {
		return new DoWorkResponse(message, bval, content, isError, isEnd);
	}

	public static DoWorkResponse from(JSONObject queried) {
		// FIXME 2021/11/24 wait for fixing.     trim here!!!
		String content = queried.getString("con");
		if (content != null) {
			content = content.replace("@@", "\n");
		}
		return create(content,
				queried.getString("m"),
				queried.getIntValue("bval"),
				queried.getString("f").equals("0"),
				queried.getString("m").equals("执行结束"));
	}
}
